﻿/// Updated on 14th Oct by cittka to migrate to .net 3.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.Profile;

public class AppInfo
{
   public DateTime Date = DateTime.Now;
   public string ServerVariables = "";
   public string Headers = "";
   public string Form = "";
   public string QueryString = "";
   public string Session = "";
   public string Files = "";
   public string UserID = "";
   public string URL = "";
   public string Browser = "";
   public string UserIP = "";
   public string UserInfo = "";
   public string MachineName = "";
   public string LastError = "";
   public string Application = "";
   public Exception LastException = null;

   public AppInfo()
   {
      Render(System.Web.HttpContext.Current);
   }

   public AppInfo(System.Web.HttpContext Current)
   {
      Render(Current);
   }

   public void Render(System.Web.HttpContext Current)
   {
      UserID = Current.User.Identity.Name;
      MachineName = Current.Server.MachineName;

      string result = "";
      HttpRequest Request = Current.Request;
      try
      {
         result = Convert.ToString(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);

         if (result.Length > 0)
            result += ",";
      }
      catch
      {
         result = "";
      }

      result += Current.Request.UserHostAddress;
      UserIP = result;

      Browser = Current.Request.UserAgent;
      URL = Current.Request.RawUrl;


      StringBuilder sb = new StringBuilder();
      sb.Append("<table border=1>");
      try
      {

         foreach (string key in Current.Request.ServerVariables.AllKeys)
         {
            sb.Append("<tr><td>" + key + "</td><td>" + Current.Request.ServerVariables[key] + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }

      ServerVariables = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);

      sb.Append("<table border=1>");
      try
      {

         foreach (string key in Current.Request.Headers.AllKeys)
         {
            sb.Append("<tr><td>" + key + "</td><td>" + Current.Request.Headers[key] + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }
      Headers = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);

      sb.Append("<table border=1>");
      try
      {

         foreach (string key in Current.Application.AllKeys)
         {
            sb.Append("<tr><td>" + key + "</td><td>" + Current.Application[key] + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }
      Application = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);


      sb.Append("<table border=1>");
      try
      {
         foreach (string key in Current.Request.Form.AllKeys)
         {
            sb.Append("<tr><td>" + key + "</td><td>" + Current.Request.Form[key] + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }
      Form = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);

      sb.Append("<table border=1>");
      try
      {
         foreach (string key in Current.Request.QueryString.AllKeys)
         {
            sb.Append("<tr><td>" + key + "</td><td>" + Current.Request.QueryString[key] + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }
      QueryString = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);

      sb.Append("<table border=1>");
      try
      {
         foreach (string key in Current.Session.Keys)
         {
            sb.Append("<tr><td>" + key + "</td><td>" + Current.Session[key].ToString() + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }
      Session = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);

      sb.Append("<table border=1>");
      try
      {
         foreach (string key in Current.Request.Files.AllKeys)
         {
            HttpPostedFile PostedFile = Current.Request.Files[key];
            sb.Append("<tr><td>" + key + "</td><td>" + PostedFile.FileName + " : " + PostedFile.ContentLength + " : " + PostedFile.ContentType + "</td></tr>");
         }
      }
      catch (Exception e) { sb.Append("<tr><td colspan=2>" + e.Message + "</td></tr>"); }
      Files = sb.ToString() + "</table>";
      sb.Remove(0, sb.Length);

      Exception ex = Current.Server.GetLastError();
      LastException = ex;
      if (ex != null)
      {
         sb.Append("---- Exception ----<br />");
         sb.Append(ex.ToString().Replace(Environment.NewLine, "<br />"));

         Exception ex1 = ex.InnerException;
         for (int i = 1; i < 5; i++)
         {
            if (ex1 == null)
               break;
            else
            {
               sb.Append("---- Inner Exception #" + i + " ----<br />");
               sb.Append(ex1.ToString().Replace(Environment.NewLine, "<br />"));
               ex1 = ex1.InnerException;
            }
         }
         LastError = sb.ToString();
      }
      sb.Remove(0, sb.Length);

   }

   public override string ToString()
   {
      StringBuilder sb = new StringBuilder();
      sb.Append("<hr >URL<hr />" + URL +
                "<hr >Exception<hr />" + (LastException == null ? "" : LastException.Message) +
                "<hr >Stack Trace<hr />" + LastError +
                "<hr >Date<hr />" + Date.ToString("dd MMM yyyy hh:mm:ss tt") +
                "<hr >UserID<hr />" + UserID +
                "<hr >Browser<hr />" + Browser +
                "<hr >QueryString<hr />" + QueryString +
                "<hr >Form<hr />" + Form +
                "<hr >UserIP<hr />" + UserIP +
                "<hr >Session<hr />" + Session +
                "<hr >MachineName<hr />" + MachineName +
                "<hr >Files<hr />" + Files +
                "<hr >Headers<hr />" + Headers +
                "<hr >Application<hr />" + Application +
                "<hr >ServerVariables<hr />" + ServerVariables +
                "");

      return sb.ToString();
   }
}